﻿using MediatR;
using W.Warehouse.Api.Application.Command.MoveCommand;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Repository.Interface;
using static WMSpublic.ParseAPI;

namespace W.Warehouse.Api.Application.CommandHandlder.MoveCommandHadler
{
    public class MoveDelectCommandHadler : IRequestHandler<MoveDelectCommand, ApiResult<int>>
    {
        private readonly IMoveProductRepository moveProductRepository;
        private readonly IMoveTheLibraryRepository moveTheLibraryRepository;
        public MoveDelectCommandHadler(IMoveProductRepository moveProductRepository, IMoveTheLibraryRepository moveTheLibraryRepository)
        {
            //产品信息
            this.moveProductRepository = moveProductRepository;
            //管理表
            this.moveTheLibraryRepository = moveTheLibraryRepository;
        }

        public async Task<ApiResult<int>> Handle(MoveDelectCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var alist = await moveTheLibraryRepository.GetAllAsync();
                var aplist = await moveProductRepository.GetAllAsync();
                var list = (from a in alist
                            join b in aplist
                            on a.MoveTheLibraryId equals b.MoveTheLibraryId
                            where b.MoveProductId == request.MoveProductId && 
                            a.IsDelete == false && b.IsDelete == false
                            select b).FirstOrDefault();
                if (list != null)
                {
                    list.IsDelete = true;
                    list.DeleteTime = new DateTime();
                }
                result.Code=await moveProductRepository.UpdateAsync(list);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
